#!/bin/bash
#J.Zh.Wu Edit version 0.1
if [ $# -ne 4 ] ; then
echo "usage:---------------------------------"
echo "./amd.sh fasta_name id_name nstruct p_trials"
echo "for example: ./amd.sh 1a68_.fasta T0003 20 1.0"
echo "---------------------------------------"
echo 
echo "exit!"
exit 1
fi

PATH_ALL="/public/home/users/qiang/rosetta3"
PATH_ALL_TEST="$PATH_ALL/test"
PATH_FASTA="$PATH_ALL/fasta"
cd $PATH_ALL_TEST
mkdir $2
DIR_PATH="$PATH_ALL_TEST/$2"
cd $DIR_PATH
mkdir frag
cd $DIR_PATH/frag

cp $PATH_FASTA/$1 .
#crete Fraglib pbs
echo "#PBS -N Fraglib.out">Frag.pbs
echo "#PBS -l nodes=1:ppn=1">>Frag.pbs
echo "#PBS -j oe">>Frag.pbs
echo "#PBS -l walltime=1000:00:00">>Frag.pbs
echo "cd $DIR_PATH/frag">>Frag.pbs
echo "/public/home/users/qiang/rosetta_fragments/nnmake/make_fragments.pl -verbose -id $2 $1 -nojufo -noprof ">>Frag.pbs
#end pbs file

#sunmit fragpbs
qsub Frag.pbs
#end

#monitor
$PATH_ALL/jc $DIR_PATH/frag aa"$2"09_05.200_v1_3
echo "fragments is finished!"
#end

#prepare
cd $DIR_PATH
mkdir pdb
cd $DIR_PATH/pdb

#create backbone flags
echo "-database $PATH_ALL/rosetta3_database">bb_flags
echo "-in:file:frag9 $DIR_PATH/frag/aa"$2"09_05.200_v1_3">>bb_flags
echo "-in:file:frag3 $DIR_PATH/frag/aa"$2"03_05.200_v1_3">>bb_flags
echo "-in:file:fasta $PATH_FASTA/$1">>bb_flags
echo "-out:nstruct $3">>bb_flags
echo "-out:file:silent "$2".silent">>bb_flags
echo "-opt_trials1 $4">>bb_flags
echo "-out:pdb">>bb_flags
#end

#create backbone pbs file
echo "#PBS -N bb.out">bb.pbs
echo "#PBS -l nodes=1:ppn=8">>bb.pbs
echo "#PBS -j oe">>bb.pbs
echo "#PBS -l walltime=1000:00:00">>bb.pbs
echo "cd $DIR_PATH/pdb">>bb.pbs
echo "$PATH_ALL/rosetta3_source/bin/aco2.linuxgccrelease @bb_flags">>bb.pbs
#end

#submit pbs
qsub bb.pbs

#monitor
for((i=1;i<="$3";i++))
do
	for j in {0..15}
	do
		if [ "$i" -lt "10" ];
		then
			$PATH_ALL/jc $DIR_PATH/pdb S_000000"$i"_"$j".pdb
		elif [ "$i" -lt "100" ];
		then
			$PATH_ALL/jc $DIR_PATH/pdb S_00000"$i"_"$j".pdb
		else
			$PATH_ALL/jc $DIR_PATH/pdb S_0000"$i"_"$j".pdb
		fi

		#monitor the one struture is finished

#		for ii in {0..15}
#		do
#			ls S*_"$ii".pdb > list"$ii"
#		done


		#create relax flags
	#-------add the relax option at there!------------------------------
		echo "-database $PATH_ALL/rosetta3_database" > r_flags"$j"
	#for example	echo "XX" >>r_flags"$jj"
	#-------end---------------------------------------------------------	
		if [ "$i" -lt "10" ];
		then
			echo "-in:file:s S_000000"$i"_"$j".pdb" >> r_flags"$j"
		elif [ "$i" -lt "100" ];
		then
			echo "-in:file:s S_00000"$i"_"$j".pdb" >> r_flags"$j"
		else
			echo "-in:file:s S_0000"$i"_"$j".pdb" >> r_flags"$j"
		fi
		
		

	#end


	#create relax pbs

		echo "#PBS -N relax"$j".out">relax"$j".pbs
		echo "#PBS -l nodes=1:ppn=1">>relax"$j".pbs
		echo "#PBS -j oe">>relax"$j".pbs
		echo "#PBS -l walltime=1000:00:00">>relax"$j".pbs
		echo "cd $DIR_PATH/pdb">>relax"$j".pbs
		echo "$PATH_ALL/rosetta3_source/bin/relax.linuxgccrelease @r_flags"$j"">>relax"$j".pbs

	#end

	#submit pbs
		qsub relax"$j".pbs

	done

done	

#monitor
for((j=1;j<="$3";j++))
do
	for t in {0..15}
	do
		if [ "$j" -lt "10" ];
		then
			$PATH_ALL/jc $DIR_PATH/pdb S_000000"$j"_"$t"_0001.pdb
		elif [ "$j" -lt "100" ];
		then
			$PATH_ALL/jc $DIR_PATH/pdb S_00000"$j"_"$t"_0001.pdb
		else
			$PATH_ALL/jc $DIR_PATH/pdb S_0000"$j"_"$t"_0001.pdb
		fi
	done
done
echo "relax is finished!"
#end

#prepare
ls S_*_0001.pdb > cluster_list

#create cluster flags
echo "-database $PATH_ALL/rosetta3_database" > c_flags
echo "-in:file:l cluster_list" >> c_flags
echo "-radius 4">>c_flags
#end

#create cluster pbs
echo "#PBS -N cluster.out">cluster.pbs
echo "#PBS -l nodes=1:ppn=1">>cluster.pbs
echo "#PBS -j oe">>cluster.pbs
echo "#PBS -l walltime=1000:00:00">>cluster.pbs
echo "cd $DIR_PATH/pdb">>cluster.pbs
echo "$PATH_ALL/rosetta3_source/bin/cluster.linuxgccrelease @c_flags">>cluster.pbs
#end

#submit pbs
qsub cluster.pbs

#monitor
$PATH_ALL/jc $DIR_PATH/pdb c.4.0.pdb
echo "cluster is finised!"
#end

#copy result
cd $DIR_PATH
mkdir result

cp $DIR_PATH/pdb/c.0.0.pdb $DIR_PATH/result/$2_1.pdb
cp $DIR_PATH/pdb/c.1.0.pdb $DIR_PATH/result/$2_2.pdb
cp $DIR_PATH/pdb/c.2.0.pdb $DIR_PATH/result/$2_3.pdb
cp $DIR_PATH/pdb/c.3.0.pdb $DIR_PATH/result/$2_4.pdb
cp $DIR_PATH/pdb/c.4.0.pdb $DIR_PATH/result/$2_5.pdb
#end all

echo "All Finished!"
echo "Please check $DIR_PATH/result dir!"


#========================================================
#assemble pdbs to ONE file
#=-=========================================================
cd $DIR_PATH/result
TargetName=$2
echo "PFRMAT   TS">${TargetName}.pdb
echo "TARGET "${TargetName}>>${TargetName}.pdb
echo "AUTHOR LenServer">>${TargetName}.pdb
echo "REMARK Predictor remarks">>${TargetName}.pdb
echo "METHOD Description of methods used">>${TargetName}.pdb
echo "METHOD Description of methods used">>${TargetName}.pdb
echo "METHOD Description of methods used">>${TargetName}.pdb

echo "MODEL  1">>${TargetName}.pdb
echo "PARENT  N/A ">>${TargetName}.pdb
cat  $DIR_PATH/result/${TargetName}_1.pdb>>${TargetName}.pdb
echo "END">>${TargetName}.pdb

echo "MODEL  2">>${TargetName}.pdb
echo "PARENT  N/A ">>${TargetName}.pdb
cat  $DIR_PATH/result/${TargetName}_2.pdb>>${TargetName}.pdb
echo "END">>${TargetName}.pdb

echo "MODEL  3">>${TargetName}.pdb
echo "PARENT  N/A ">>${TargetName}.pdb
cat  $DIR_PATH/result/${TargetName}_3.pdb>>${TargetName}.pdb
echo "END">>${TargetName}.pdb


echo "MODEL  4">>${TargetName}.pdb
echo "PARENT  N/A ">>${TargetName}.pdb
cat  $DIR_PATH/result/${TargetName}_4.pdb>>${TargetName}.pdb
echo "END">>${TargetName}.pdb

echo "MODEL  5">>${TargetName}.pdb
echo "PARENT  N/A ">>${TargetName}.pdb
cat  $DIR_PATH/result/${TargetName}_5.pdb>>${TargetName}.pdb
echo "END">>${TargetName}.pdb

cd $PATH_ALL
./send.sh $TargetName


